package fragment;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lidroid.xutils.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

import app.ContactInfo;
import app.Main;
import app.ContactsSearch;
import application.AppApplication;
import custom.PinnedSectionListView;
import fragment.contact.ContactsAdapter;
import fragment.contact.ContactsPersenter;
import mainUI.AppFragment;
import menhoo.com.i8officelibrary.R;
import model.EmployeeItemDBModel;

/**
 * Created by zhukang on 2018/6/21.
 */

public class Contacts1 extends AppFragment implements View.OnClickListener {
    private static final String TAG = "Contacts1";
    private ContactsPersenter persenter;
    private FragmentManager fragmentManager;
    private PinnedSectionListView list;
    private List<EmployeeItemDBModel> ListData;
    private ContactsAdapter sadapter;
    private boolean isFastScroll = false;
    private boolean isShadowVisible = false;
    private boolean isEdit = false;
    private boolean IsMultiple = true;
    private LinearLayout searchLayout;

    @Override
    public boolean onBackPressed() {
        String deptID = persenter.getListDataDataPreviousByNet();

        LogUtils.e("Contacts onBackPressed:" + deptID);
        LogUtils.e("Contacts:" + AppApplication.getUser().EmplID);

        if (deptID!=null&&deptID.equalsIgnoreCase("0")) {
            setMainShowBack();
        }

        if (TextUtils.isEmpty(deptID)) {
            return false;
        } else {
            return true;
        }
    }

    private void setMainShowBack() {
        if (getActivity().getClass() == Main.class) {
            showBackBtn(false);
        }
    }

    @Override
    public void onClick(View view) {
        if(view == searchLayout){
            Intent it = new Intent(context, ContactsSearch.class);
            startActivity(it);
        }
    }

    /**
     * 是否显示返回按钮
     * @param isShow
     */
    private void showBackBtn(Boolean isShow){
        setBtnBackDisplay(isShow);
        //如果要显示返回按钮，隐藏搜索框
        if(isShow){
            searchLayout.setVisibility(View.GONE);
        }else{
            searchLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContextView(R.layout.fragment_contacts1);
        persenter = new ContactsPersenter(this);
        fragmentManager = getFragmentManager();
    }

    @Override
    public void init() {
        super.init();
        initView();

        ArrayList<EmployeeItemDBModel> DataList = initData();

        list.setShadowVisible(isShadowVisible);
//        initListHeaderAndFooter();//原来的所有人和群聊放入list的adapter可适配的部分
        hideContent();
        persenter.initFirstListData(DataList);
    }

    private ArrayList<EmployeeItemDBModel> initData() {
        ArrayList<EmployeeItemDBModel> dataList = null;
        if (IsReturnResult) {
            try {
                IsMultiple = TagBundle.getBoolean("IsMultiple");
                dataList = (ArrayList<EmployeeItemDBModel>) TagBundle.getSerializable("DataList");
            } catch (Exception ex) {

            }
        }
        return dataList;
    }

    private void initView() {
        searchLayout = (LinearLayout)getRootView().findViewById(R.id.searchLayout);
        searchLayout.setOnClickListener(this);
        list = (PinnedSectionListView) getRootView().findViewById(R.id.PinnedSectionList);
        ListData = new ArrayList<EmployeeItemDBModel>();
        sadapter = new ContactsAdapter(getActivity(), ListData, isEdit);
        list.setAdapter(sadapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //如果id是-1说明这个是Header 或者  Footer
                EmployeeItemDBModel item = (EmployeeItemDBModel) parent.getAdapter().getItem(position);
                if (TextUtils.isEmpty(item.EmplID) && TextUtils.isEmpty(item.DeptID)) {
                    LogUtils.e("no EmplID and no DeptID");
                } else {
                    //如果是选择模式 并且是人员，则设置选中与不选中
                    if (isEdit && !TextUtils.isEmpty(item.EmplID)) {
                        if (item.type == EmployeeItemDBModel.ITEM) {
                            if (!IsMultiple) {
                                sadapter.clearSelected();
                                sadapter.notifyDataSetChanged();
                            }
                            item.checked = !item.checked;
                            if (item.checked) {
                                ((ImageView) view.findViewById(R.id.imageViewCheck)).setImageResource(R.drawable.selecter_selected_icon);
                            } else {
                                ((ImageView) view.findViewById(R.id.imageViewCheck)).setImageResource(R.drawable.selecter_unselected_icon);
                            }
                            persenter.setSelectEmpl(item);
                        }
                    } else {
                        //判断点击的是标题头还是真正的选项
                        if (item.type == EmployeeItemDBModel.ITEM) {
                            if (!TextUtils.isEmpty(item.DeptID)) {
                                showBackBtn(true);
                                persenter.getListDeptDataByNet(item.DeptID, true);
                            } else if(!TextUtils.isEmpty(item.EmplID)) {
                                Intent it = new Intent(context, ContactInfo.class);
                                it.putExtra("emplID", item.EmplID);
                                startActivity(it);
                            }
                        }
                    }
                }
            }

        });
    }

    public void setAdapterNotifyDataSetChanged(List<EmployeeItemDBModel> listData) {
        ListData.clear();
        ListData.addAll(listData);
        sadapter.notifyDataSetChanged();
        list.setFastScrollEnabled(isFastScroll);
        if (isFastScroll) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                list.setFastScrollAlwaysVisible(true);
            }
        }
//      if(footer!=null){
//          footer.setText("{0}位联系人".replace("{0}", sadapter.getItemTypeCount()+""));
//      }
        showContent();
        hideAll(500);
    }
}
